package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.Question;
import com.jyy.prefabricated.entity.QuestionBank;
import com.jyy.prefabricated.entity.QuestionBankCategory;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.QuestionBankMapper;
import com.jyy.prefabricated.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class QuestionBankService {

    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private QuestionBankCategoryService questionBankCategoryService;
    @Resource
    private QuestionMapper questionMapper;


    /**
     * 新增
     *
     * @param questionBank
     * @return
     */
    public int insert(QuestionBank questionBank) {
        questionBankMapper.insert(questionBank);
        return questionBank.getId();
    }

    /**
     * 更新
     *
     * @param questionBank
     * @return
     */
    public int update(QuestionBank questionBank) {
        if (questionBank.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        return questionBankMapper.updateByPrimaryKeySelective(questionBank);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        QuestionBank questionBank = questionBankMapper.selectByPrimaryKey(id);
        questionBank.setDeleted(true);
        return questionBankMapper.updateByPrimaryKeySelective(questionBank);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public QuestionBank selectById(Integer id) {
        return questionBankMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有题库
     *
     * @return
     */
    public List<QuestionBank> selectAll() {
        return questionBankMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     *
     * @param name       名称
     * @param type       类型
     * @param categoryId 分类id
     * @return
     */
    public List<QuestionBank> selectByParams(String name, Integer type, Integer categoryId) {
        return questionBankMapper.selectByParams(name, type, categoryId);
    }

    /**
     * 根据参数查询列表
     *
     * @param name        名称
     * @param type        类型
     * @param categoryIds 分类id列表
     * @return
     */
    public List<QuestionBank> selectDeepByParams(List<Integer> categoryIds, String name, Integer type, Integer questionState, String remark, Integer trainingOrganizationId) {
        List<QuestionBank> result = questionBankMapper.selectByParamsDeep(categoryIds, name, type, questionState, remark, trainingOrganizationId);
        if (result != null && result.size() > 0) {
            for (QuestionBank questionBank : result) {
                List<Question> questionList = questionMapper.selectByParams(questionBank.getId(), null, null, null, null, null, null, null, null);
                if (questionList != null && questionList.size() > 0) {
                    questionBank.setQuestionCount(questionList.size());
                } else {
                    questionBank.setQuestionCount(0);
                }
            }
        }
        return result;
    }

    public List<QuestionBank> selectTrainingOrganizationQuestionBankList(Integer trainingOrganizationId) {

        return questionBankMapper.selectTrainingOrganizationQuestionBankList(trainingOrganizationId);
    }

    /**
     * 根据分类上下级关系查询分类列表
     *
     * @param categoryId
     * @return
     */
    public List<Integer> selectDeepCategoryIds(Integer categoryId) {
        List<Integer> categoryIds = new ArrayList<>();
        if (categoryId != null) {
            List<QuestionBankCategory> list = questionBankCategoryService.selectAll();
            List<QuestionBankCategory> categories = questionBankCategoryService.findCategoryIdByPid(categoryId, list);
            categoryIds.addAll(categories.stream().map(e -> e.getId()).collect(Collectors.toList()));
            categoryIds.add(categoryId);
        }
        return categoryIds;
    }
}
